package caidan.data;

import java.awt.GridLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JPanel;

import jinyilw.common.Operator;

public class FoodProperty
{
	// 口味
	public static final String[] foodFlavour = {"酸", "甜", "咸", "辣", "鲜", "脆"};
	public static final int[] flavourConflictIndexs = {0, 1};
	// 颜色
	public static final String[] foodColor = {"红", "绿", "黄", "紫", "白", "橙",
			"灰"};
	public static final int[] colorConflictIndexs = {3, 4, 6};
	// 时令
	public static final String[] foodSeason = {"春", "夏", "秋", "冬"};
	// 辣度
	public static final String[] foodPeppery = {"不辣", "微辣", "中辣", "较辣"};

	private static int maxConflictNum = 3;
	public static int currentSeasonIndex = FoodProperty.getCurrentSeasonIndex();

	private StringBuilder flavour;
	private StringBuilder color;
	private StringBuilder season;

	final boolean[] flavourDatas = new boolean[foodFlavour.length];
	final boolean[] colorDatas = new boolean[foodColor.length];
	final boolean[] seasonDatas = new boolean[foodSeason.length];
	short pepperyData = -1;

	public FoodProperty()
	{
	}

	public static int getCurrentSeasonIndex()
	{
		int seasonNumber = Calendar.getInstance().get(Calendar.MONTH);
		if (seasonNumber >= 1 && seasonNumber <= 3)
			return 0;
		if (seasonNumber >= 4 && seasonNumber <= 6)
			return 1;
		if (seasonNumber >= 7 && seasonNumber <= 9)
			return 2;
		return 3;
	}

	public boolean isFlavourSetted()
	{
		for (boolean b : flavourDatas)
			if (b)
				return true;
		return false;
	}

	public boolean isColorSetted()
	{
		for (boolean b : colorDatas)
			if (b)
				return true;
		return false;
	}

	public boolean isSeasonSetted()
	{
		for (boolean b : seasonDatas)
			if (b)
				return true;
		return false;
	}

	public boolean isPepperySetted()
	{
		return pepperyData >= 0;
	}

	public boolean isFlavourConflict(ArrayList<FoodData> oneMealDatas)
	{
		int count;
		for (int index : flavourConflictIndexs)
			if (flavourDatas[index])
			{
				count = 1;
				for (FoodData foodData : oneMealDatas)
					if (foodData.property.flavourDatas[index])
					{
						count++;
						if (count > maxConflictNum)
							return true;
					}
			}
		return false;
	}

	public boolean isColorConflict(ArrayList<FoodData> oneMealDatas)
	{
		int count;
		for (int index : colorConflictIndexs)
			if (colorDatas[index])
			{
				count = 1;
				for (FoodData foodData : oneMealDatas)
					if (foodData.property.colorDatas[index])
					{
						count++;
						if (count > maxConflictNum)
							return true;
					}
			}
		return false;
	}

	public boolean isSeasonConflict()
	{
		return isSeasonSetted() && !seasonDatas[currentSeasonIndex];
	}

	public void setFoodProperty(FoodProperty property)
	{
		for (int i = 0; i < flavourDatas.length; i++)
			flavourDatas[i] = property.flavourDatas[i];
		for (int i = 0; i < colorDatas.length; i++)
			colorDatas[i] = property.colorDatas[i];
		for (int i = 0; i < seasonDatas.length; i++)
			seasonDatas[i] = property.seasonDatas[i];
		pepperyData = property.pepperyData;
	}

	public void clear()
	{
		for (int i = 0; i < flavourDatas.length; i++)
			flavourDatas[i] = false;
		for (int i = 0; i < colorDatas.length; i++)
			colorDatas[i] = false;
		for (int i = 0; i < seasonDatas.length; i++)
			seasonDatas[i] = false;
		pepperyData = -1;
	}

	public void setFlavourData(int index, boolean b)
	{
		flavourDatas[index] = b;
		flavour = null;
	}

	public String getFlavour()
	{
		if (flavour == null)
		{
			flavour = new StringBuilder();
			for (int i = 0; i < foodFlavour.length; i++)
				if (flavourDatas[i])
					flavour.append(foodFlavour[i]);
		}
		return flavour.toString();
	}

	public void setColorData(int index, boolean b)
	{
		colorDatas[index] = b;
		color = null;
	}

	public String getColor()
	{
		if (color == null)
		{
			color = new StringBuilder();
			for (int i = 0; i < foodColor.length; i++)
				if (colorDatas[i])
					color.append(foodColor[i]);
		}
		return color.toString();
	}

	public void setSeasonData(int index, boolean b)
	{
		seasonDatas[index] = b;
		season = null;
	}

	public String getSeason()
	{
		if (season == null)
		{
			season = new StringBuilder();
			for (int i = 0; i < foodSeason.length; i++)
				if (seasonDatas[i])
					season.append(foodSeason[i]);
		}
		return season.toString();
	}

	public void setPepperyData(short index)
	{
		pepperyData = index;
	}

	public String getPeppery()
	{
		return foodPeppery[pepperyData];
	}

	void writeData(DataOutputStream dos) throws IOException
	{
		dos.writeShort(flavourDatas.length);
		for (boolean b : flavourDatas)
			dos.writeBoolean(b);
		dos.writeShort(colorDatas.length);
		for (boolean b : colorDatas)
			dos.writeBoolean(b);
		dos.writeShort(seasonDatas.length);
		for (boolean b : seasonDatas)
			dos.writeBoolean(b);
		dos.writeShort(pepperyData);
	}

	void readData(DataInputStream dis, int appVersion) throws IOException
	{
		short length = dis.readShort();
		for (int i = 0; i < length; i++)
			flavourDatas[i] = dis.readBoolean();
		length = dis.readShort();
		for (int i = 0; i < length; i++)
			colorDatas[i] = dis.readBoolean();
		length = dis.readShort();
		for (int i = 0; i < length; i++)
			seasonDatas[i] = dis.readBoolean();
		pepperyData = dis.readShort();
	}

	public static class FoodPropertyPanel extends JPanel
	{
		private static final long serialVersionUID = -7778470189876587862L;
		private FoodProperty foodProperty;
//		private JCheckBox[] flavourBoxs = new JCheckBox[foodFlavour.length];
//		private JCheckBox[] colorBoxs = new JCheckBox[foodColor.length];
//		private JCheckBox[] seasonBoxs = new JCheckBox[foodSeason.length];
//		private JRadioButton[] pepperyButtons = new JRadioButton[foodPeppery.length];

		private CheckPanel flavourPanel, colorPanel, seasonPanel;
		private RadioPanel pepperyPanel;

		public FoodPropertyPanel()
		{
			super(new GridLayout(0, 1, 0, 3));
			flavourPanel = new CheckPanel("口味：", foodFlavour);
			colorPanel = new CheckPanel("颜色：", foodColor);
			seasonPanel = new CheckPanel("季节：", foodSeason);
			pepperyPanel = new RadioPanel("辣度：", foodPeppery,
					new Operator<Short>()
					{
						@Override
						public void operate(Short o)
						{
							foodProperty.setPepperyData(o);
						}
					});

			add(flavourPanel);
			add(colorPanel);
			add(seasonPanel);
			add(pepperyPanel);
		}

		public void setFoodProperty(FoodProperty property)
		{
			foodProperty = property;
			flavourPanel.setDatas(property.flavourDatas);
			colorPanel.setDatas(property.colorDatas);
			seasonPanel.setDatas(property.seasonDatas);
			pepperyPanel.setData(property.pepperyData);
		}
	}
}
